package com.tdam2013.grupo07.conectivity;

import java.util.HashMap;
import java.util.Iterator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.tdam2013.grupo07.util.Constantes;


public class ConectividadListener {

	
	private Context mContext;
	
	private State state;

	private HashMap<Handler, Integer> handlers = new HashMap<Handler, Integer>();

	private boolean mListening;

	private ConectividadBroadcastReciever conectividadBroadcastReciever;
	
	public enum State {
		UNKNOWN,

		CONNECTED,
		
		NOT_CONNECTED
	}

	public ConectividadListener() 
	{
		state = State.UNKNOWN;
		conectividadBroadcastReciever = new ConectividadBroadcastReciever();
	}
	
	public State getState()
	{
		return state;
	}

	/**
	 * This method starts listening for network connectivity state changes.
	 * 
	 * @param context
	 */
	public synchronized void startListening(Context context) {
		if (!mListening) {
			mContext = context;
			IntentFilter filter = new IntentFilter();
			filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
			context.registerReceiver(conectividadBroadcastReciever, filter);
			mListening = true;
		}
	}

	/**
	 * This method stops this class from listening for network changes.
	 */
	public synchronized void stopListening() 
	{
		if (mListening) 
		{
			mContext.unregisterReceiver(conectividadBroadcastReciever);
			mListening=false;
		}
	}

	/**
	 * This methods registers a Handler to be called back onto with the
	 * specified what code when the network connectivity state changes.
	 * 
	 * @param target
	 *            The target handler.
	 * @param what
	 *            The what code to be used when posting a message to the
	 *            handler.
	 */
	public void registerHandler(Handler target, int what) {
		handlers.put(target, what);
	}

	/**
	 * This methods unregisters the specified Handler.
	 * 
	 * @param target
	 */
	public void unregisterHandler(Handler target) {
		handlers.remove(target);
	}
	
	private class ConectividadBroadcastReciever extends BroadcastReceiver 
	{
		@Override
		public void onReceive(Context context, Intent intent) 
		{
			String action = intent.getAction();
			if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) 
			{
				boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
				if (noConnectivity) 
				{
					state = State.NOT_CONNECTED;
				} 
				else 
				{
					ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
					NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
					if(networkInfo.getType()==ConnectivityManager.TYPE_MOBILE || networkInfo.getType()==ConnectivityManager.TYPE_WIFI)
						state = State.CONNECTED;
				}
			}
		}
	};

}
